﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;

namespace Xceed.Silverlight.DataGrid
{
  public class LicensingRichTextBox : RichTextBox
  {

    #region LicensingParagraph

    public Paragraph LicensingParagraph
    {
      get
      {
        return m_Paragraph;
      }
      set
      {
        m_Paragraph = value;
        AddParagraphToRichTextBox();
      }
    }

    private Paragraph m_Paragraph;

    #endregion


    override public void OnApplyTemplate()
    {
      base.OnApplyTemplate();
      ParseParagraph();
    }


    private void ParseParagraph()
    {
      IEnumerator<Inline> paragraphEnumerator = m_Paragraph.Inlines.GetEnumerator();
      while( paragraphEnumerator.MoveNext() )
      {
        if( paragraphEnumerator.Current.GetType() == typeof( Hyperlink ) )
        {
          Hyperlink hyperlink = ( Hyperlink )paragraphEnumerator.Current;

          Color col = new Color();
          col = Color.FromArgb( 255, 85, 0, 0 );
          SolidColorBrush mouseOverForegroundBrush = new SolidColorBrush( col );

#if SILVERLIGHT
          hyperlink.Foreground = new SolidColorBrush( Colors.White );
          hyperlink.MouseOverForeground = mouseOverForegroundBrush;
#else
          Style style = new Style();
          style.TargetType = typeof( Hyperlink );
          Trigger trigger = new Trigger();
          trigger.Property = Hyperlink.IsMouseOverProperty;
          trigger.Value = true;

          Setter setterColor = new Setter( Hyperlink.ForegroundProperty, mouseOverForegroundBrush );
          Setter setterCursor = new Setter( Hyperlink.CursorProperty, Cursors.Hand );
          trigger.Setters.Add( setterColor );
          trigger.Setters.Add( setterCursor );

          style.Triggers.Add( trigger );
          paragraphEnumerator.Current.Style = style;

          //Manage hyperlink click and uri path          
          hyperlink.MouseLeftButtonDown += this.OnHyperlinkClick;
          m_HyperlinkNavigateUri = hyperlink.NavigateUri.AbsoluteUri;
#endif
        }
      }
    }

#if !SILVERLIGHT
    private void OnHyperlinkClick( object sender, EventArgs e )
    {
      ProcessStartInfo startInfo = new ProcessStartInfo( m_HyperlinkNavigateUri );
      Process.Start( startInfo );
    }
#endif


    private void AddParagraphToRichTextBox()
    {
#if SILVERLIGHT
      this.Blocks.Clear();
      this.Blocks.Add( m_Paragraph );
#else
      FlowDocument flowDoc = new FlowDocument();
      flowDoc.Blocks.Clear();
      flowDoc.Blocks.Add( m_Paragraph );

      this.Document = flowDoc;
#endif
    }

#if !SILVERLIGHT
    private string m_HyperlinkNavigateUri;
#endif

  }
}
